import { menuItemEmits } from "element-plus";

interface ILayerList {
  id: string;
  name: string;
  serviceUrl: string;
  serviceType: number;
}
// 已选图层记录
const state: any = {
  layerList: [],
  collectLayerList: [],
  resultList: [], //工具栏中属性查询结果
};

const mutations = {
  addLayer: (state: any, action: any) => {
    state.layerList.push(action.value);
  },
  removeLayer: (state: any, action: any) => {
    let index = state.layerList.findIndex(
      (item: any) => item.id == action.value.id
    );
    state.layerList.splice(index, 1);
  },
  initLayer: (state: any, action: any) => {
    state.layerList = action.value;
  },
  filterLayer: (state: any, action: any) => {
    state.layerList = state.layerList.filter(
      (item: any) => item.name.indexOf(action.value) !== -1
    );
  },
  changeCollectLayer: (state: any, action: any) => {
    state.collectLayerList = action.value;
  },
  filterCollectLayer: (state: any, action: any) => {
    if (action.value != "") {
      state.collectLayerList = state.collectLayerList.filter(
        (item: any) => item.name.indexOf(action.value) !== -1
      );
    }
  },
  initResult: (state: any, action: any) => {
    state.resultList = action.value;
  },
  addResult: (state: any, action: any) => {
    state.resultList.push(action.value);
  },
};
const getters = {
  getLayerList: (state: any) => {
    return state.layerList;
  },
  getCollectLayerList: (state: any) => {
    // return state.collectLayerList;
    if (state.layerList.length > 0) {
      let newList: any = [];
      state.collectLayerList.forEach((coll: any) => {
        let index = state.layerList.findIndex(
          (item: any) => item.id == coll.id
        );
        if (index > -1) {
          coll.open = true;
        } else {
          coll.open = false;
        }
        newList.push(coll);
      });
      return newList;
    } else {
      return state.collectLayerList.map((item: any) => {
        item.open = false;
        return item;
      });
    }
  },
  getResultList: (state: any) => {
    return state.resultList;
  },
};
const actions = {
  ADD_LAYER: (dispatch: any, value: any) => {
    let action = {
      type: "addLayer",
      value,
    };
    dispatch.commit(action);
  },
  REMOVE_LAYER: (dispatch: any, value: any) => {
    let action = {
      type: "removeLayer",
      value,
    };
    dispatch.commit(action);
  },
  INIT_LAYER: (dispatch: any, value: any) => {
    let action = {
      type: "initLayer",
      value,
    };
    dispatch.commit(action);
  },
  FILTER_LAYER: (dispatch: any, value: any) => {
    let action = {
      type: "filterLayer",
      value,
    };
    dispatch.commit(action);
  },
  CHANGE_COLLECT_LAYER: (dispatch: any, value: any) => {
    let action = {
      type: "changeCollectLayer",
      value,
    };
    dispatch.commit(action);
  },
  FILTER_COLLECT_LAYER: (dispatch: any, value: any) => {
    let action = {
      type: "filterCollectLayer",
      value,
    };
    dispatch.commit(action);
  },
  INIT_RESULT: (dispatch: any, value: any) => {
    let action = {
      type: "initResult",
      value,
    };
    dispatch.commit(action);
  },
  ADD_RESULT: (dispatch: any, value: any) => {
    let action = {
      type: "addResult",
      value,
    };
    dispatch.commit(action);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions,
};
